﻿using System.Text;
using System.Collections.Generic;
using System;
using System.IO;
using System.Globalization;

using SiteUnity.Layers.Application;
using SiteUnity.Layers.Application.BaseClasses;
using SiteUnity.Layers.Entity.General;
using SiteUnity.Frame.Common.UITools;
using SiteUnity.Frame.Common.Tools.Data;

namespace SiteUnity.Frame.Modules
{
    [System.Web.UI.ToolboxData("<{0}:Menu runat=server></{0}:Menu>")]
    public class Menu : BaseWebControl
    {
        private int _expandLevel = 0;
        private int _tmpExpandLevel = 0;
        public int ExpandLevel { get { return _expandLevel; } set { _expandLevel = value; } }

        public View View { get; set; }

        private List<Page> _pages = SiteManager.Website.Pages;
        public List<Page> Pages { get { return _pages; } set { _pages = value; } }

        private void GenerateMenu(System.Web.UI.HtmlTextWriter output, List<Page> childPage)
        {
            if (childPage.Count != 0)
            {
                if (ExpandLevel < 0)
                    return;

                //output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Id, this.ID);
                //output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Class, View.ToString());
                output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Ul);
                foreach (SiteUnity.Layers.Entity.General.Page page in childPage)
                {
                    if (_expandLevel == _tmpExpandLevel && page.ParentGuid != default(Guid))
                        continue;

                    if (page.ChildPages.Count != 0 && ExpandLevel > 0)
                        output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Class, "hasSub");

                    output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Title, page.Title);
                    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
                    GenerateMenuNode(output, page);
                    ExpandLevel--;
                    GenerateMenu(output, page.ChildPages);
                    ExpandLevel++;
                    output.RenderEndTag();
                }
                output.RenderEndTag();
            }
        }

        private void GenerateMenuNode(System.Web.UI.HtmlTextWriter output, SiteUnity.Layers.Entity.General.Page page)
        {
            output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Href, page.Url);
            output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.A);
            output.Write(page.Title);
            output.RenderEndTag();
        }

        protected override void OnPreRender(EventArgs e)
        {
            //this.Attributes.Add("class", "Menu" + View.ToString());

            Page.ClientScript.RegisterClientScriptBlock(typeof(System.Web.UI.MasterPage), this.ClientID, string.Format("var PageUrl = '{0}'; var MenuId = '{1}';", SiteManager.Website.CurrentPage.Url, this.ClientID), true);
        }

        protected override void RenderContents(System.Web.UI.HtmlTextWriter output)
        {
            string cacheKey = "Menu" + this.ID;
            object content;            

            if (!Cache.Get(cacheKey, out content))
            {
                StringBuilder sb = new StringBuilder();
                System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(new StringWriter(sb, CultureInfo.InvariantCulture));

                _tmpExpandLevel = _expandLevel;
                GenerateMenu(htw, _pages);

                Cache.Add(cacheKey, sb.ToString());
                output.Write(sb.ToString());
            }
            else
            {
                output.Write(content);
            }
        }
    }
}
